﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace HuoTu.Common
{
    /// <summary>
    /// 客户端操作
    /// </summary>
    public class SysScript
    {        
        /// <summary>
        /// 在客户端弹出确定对话框
        /// </summary>
        /// <param name="page">当前页面</param>
        /// <param name="msgContent">弹出的内容</param>
        public static void MsgBox(Page page, string msgContent)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>alert('");
            sb.Append(msgContent);
            sb.Append("')</script>");
            ClientScriptManager csm = page.ClientScript;
            if (!csm.IsStartupScriptRegistered("msgbox"))
            {
                csm.RegisterStartupScript(page.GetType(), "msgbox", sb.ToString());
            }
        }

        /// <summary>
        /// 在客户端弹出选择对话框，确定执行okOperation，取消执行cancelOperation
        /// </summary>
        /// <param name="page">当前页面的指针，一般为this</param>
        /// <param name="msgContent">弹出的内容</param>
        /// <param name="okOperation">确定执行的操作</param>
        /// <param name="cancelOperation">取消执行的操作</param>
        public static void ConfirmBox(Page page, string msgContent, string okOperation, string cancelOperation)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>var cf=confirm('");
            sb.Append(msgContent);
            sb.Append("');if(cf){");
            sb.Append(okOperation);
            sb.Append("}else{");
            sb.Append(cancelOperation);
            sb.Append("}</script>");

            ClientScriptManager csm = page.ClientScript;
            if (!csm.IsStartupScriptRegistered("confirmbox"))
            {
                csm.RegisterStartupScript(page.GetType(), "confirmbox", sb.ToString());
            }
        }

        /// <summary>
        /// 向页面注册JavaScript脚本
        /// </summary>
        /// <param name="page">当前页面的指针，一般为this</param>
        /// <param name="scriptName">脚本名称</param>
        /// <param name="scriptContent">脚本内容</param>
        /// <param name="regPlace">脚本注册位置：begin(开始位置)，end(结束位置)</param>
        public static void RegScript(Page page, string scriptName, string scriptContent, string regPlace)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>");
            sb.Append(scriptContent);
            sb.Append("</script>");

            ClientScriptManager csm = page.ClientScript;
            switch (regPlace)
            {
                case "begin":
                    if (!csm.IsStartupScriptRegistered(scriptName))
                    {
                        csm.RegisterStartupScript(page.GetType(), scriptName, sb.ToString());
                    }
                    break;
                case "end":
                    if (!csm.IsClientScriptBlockRegistered(scriptName))
                    {
                        csm.RegisterClientScriptBlock(page.GetType(), scriptName, sb.ToString());
                    }
                    break;
                default:
                    if (!csm.IsStartupScriptRegistered(scriptName))
                    {
                        csm.RegisterStartupScript(page.GetType(), scriptName, sb.ToString());
                    }
                    break;
            }
        }
    }
}
